<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header content="增加月卡" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="userInfoform" label-width="100px" :model="userInfo" :rules="userInfoRules">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="userInfo.personName" size="mini" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="userInfo.phoneNumber" size="mini" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-model="userInfo.carNumber" size="mini" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="userInfo.carBrand" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="cardInfoform" :model="cardInfo" :rules="cardInfoRules" label-width="100px">
            <el-form-item label="有效日期" prop="value">
              <el-date-picker
                v-model="cardInfo.value"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                size="mini"
                @change="onChange"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="cardInfo.paymentAmount" size="mini" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="cardInfo.paymentMethod" size="mini">
                <el-option
                  v-for="item in [{
                    industryCode: 'Cash',
                    industryName: '现下'
                  }, {
                    industryCode: 'Alipay',
                    industryName: '支付宝'
                  }, {
                    industryCode: 'WeChat',
                    industryName: '微信'
                  }
                  ]"
                  :key="item.industryCode"
                  :value="item.industryCode"
                  :label="item.industryName"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="addCar">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addCar, getCarInfo, updateCar } from '@/api/car/carCard'
export default {
  name: 'AddCard',
  data() {
    return {
      userInfo: {
        personName: '',
        phoneNumber: '',
        carNumber: '',
        carBrand: ''
      },
      userInfoRules: {
        personName: [
          { required: true, message: '请输入车主姓名', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' } // 正则校验手机号格式
        ],
        carNumber: [
          { required: true, message: '请输入车辆号码', trigger: 'blur' },
          // 校验车牌号
          { pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/, message: '请输入正确的车牌号', trigger: 'blur' }
        ],
        carBrand: [
          { required: true, message: '请输入车辆品牌', trigger: 'blur' }]
      },
      cardInfo: {
        cardStartDate: '',
        cardEndDate: '',
        paymentAmount: '',
        paymentMethod: '',
        value: ''
      },
      cardInfoRules: {
        cardStartDate: [
          { required: true, message: '请选择有效日期', trigger: 'blur' }
        ],
        cardEndDate: [
          { required: true, message: '请选择有效日期', trigger: 'blur' }
        ],
        paymentAmount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请选择支付方式', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '请选择有效日期', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    // console.log(this.$route.query.id)
    this.$route.query.id && this.getCarInfo()
  },
  methods: {
    onChange(value) {
    //   console.log(value)
      this.cardInfo.cardStartDate = value[0]
      this.cardInfo.cardEndDate = value[1]
    },
    // 提交表单
    async addCar() {
      await this.$refs['userInfoform'].validate()
      await this.$refs['cardInfoform'].validate()
      delete this.cardInfo.value
      if (this.$route.query.id) {
        await updateCar({
          ...this.userInfo,
          ...this.cardInfo
        })
        this.$message.success('修改成功')
      } else {
        await addCar({
          ...this.userInfo,
          ...this.cardInfo
        })
        this.$message.success('添加成功')
      }
      this.$router.back()
    },
    async getCarInfo() {
      const res = await getCarInfo(this.$route.query.id)
      console.log(res)
      // this.userInfo = data
      // 回填车辆信息表单
      const { carInfoId, personName, phoneNumber, carNumber, carBrand } = res
      this.userInfo = {
        personName, phoneNumber, carNumber, carBrand, carInfoId
      }

      // 回填缴费信息表单
      const { rechargeId, cardStartDate, cardEndDate, paymentAmount, paymentMethod } = res
      this.cardInfo = {
        rechargeId,
        paymentAmount,
        paymentMethod,
        value: [cardStartDate, cardEndDate],
        cardStartDate,
        cardEndDate
      }
    },
    // 重置表单
    resetForm() {
      this.$refs['userInfoform'].resetFields()
      this.$refs['cardInfoform'].resetFields()
      this.userInfo = {
        personName: '',
        phoneNumber: '',
        carNumber: '',
        carBrand: ''
      }
      this.cardInfo = {
        cardStartDate: '',
        cardEndDate: '',
        paymentAmount: '',
        paymentMethod: '',
        value: ''
      }
    }
  }
}
</script>

<style scoped lang="scss">
.add-card {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
        display: flex;
        align-items: center;
        padding: 0 20px;
        height: 64px;
        background-color: #fff;

        .left {
            span {
                margin-right: 4px;
            }

            .arrow {
                cursor: pointer;
            }
        }

        .right {
            text-align: right;
        }
    }

    .add-main {
        background: #f4f6f8;
        padding: 20px 130px;

        .form-container {
            background-color: #fff;

            .title {
                height: 60px;
                line-height: 60px;
                padding-left: 20px;
            }

            .form {
                margin-bottom: 20px;
                padding: 20px 65px 24px;

                .el-form {
                    display: flex;
                    flex-wrap: wrap;

                    .el-form-item {
                        width: 50%;
                    }
                }
            }
        }

        .preview {
            img {
                width: 100px;
            }
        }
    }

    .add-footer {
        position: fixed;
        bottom: 0;
        width: 100%;
        padding: 24px 50px;
        color: #000000d9;
        font-size: 14px;
        background: #fff;
        text-align: center;
    }
}
</style>
